﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SjclHelpers.Codec;
using SjclHelpers.Misc;

namespace SjclHelpersTests.Misc
{
    [TestClass]
    public class PBKDF2HMACSHA256ExtensionsTests
    {
        private const string ValidSalt = "23c2618b94d2e506";

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void DeriveKey_EmptyPassword_ArgumentOutOfRangeException()
        {
            // Fixture setup...

            // Exercise and verify SUT...
            "".DeriveKey(ValidSalt.ToBytes());
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void DeriveKey_NoSalt_ArgumentNullException()
        {
            // Fixture setup...

            // Exercise and verify SUT...
            "password".DeriveKey(null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void DeriveKey_KeyLengthNotMultipleOf8_ArgumentOutOfRangeException()
        {
            // Fixture setup...

            // Exercise and verify SUT...
            "password".DeriveKey(ValidSalt.ToBytes(), keyLength: 7);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void DeriveKey_KeyLength0_ArgumentOutOfRangeException()
        {
            // Fixture setup...

            // Exercise and verify SUT...
            "password".DeriveKey(ValidSalt.ToBytes(), keyLength: 0);
        }

        [TestMethod]
        public void DeriveKey_Works()
        {
            // Fixture setup...
            var salt = "23c2618b94d2e506".ToBytes();
        
            // Exercise SUT...
            var derivedKey = "password".DeriveKey(salt);
        
            // Verify SUT...
            Assert.AreEqual(
                "98eae32d8bab9be0f3a0d9ac3dbd1612",
                derivedKey.ToHex()
            );
        }
    }
}
